package com.gen.utils.ngen;

import org.yaml.snakeyaml.Yaml;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Random;

public class KRNameGenUtils {
    private static final String KANA_TO_ROMAJI_FILE = "korea_names.yml";
    private static LinkedHashMap nameinfo;
    private static  List<String> familyNames;
    private static  List<String> femaleNames;
    private static  List<String> maleNames;


    static {
        try{
            Yaml yaml = new Yaml();
            nameinfo = yaml.load(JpNameGenUtils.class.getClassLoader()
                    .getResourceAsStream(KANA_TO_ROMAJI_FILE));
            familyNames = (List<String>) nameinfo.get("family_name");
            femaleNames = (List<String>) ((LinkedHashMap) nameinfo.get("first_name")).get("female");
            maleNames = (List<String>) ((LinkedHashMap) nameinfo.get("first_name")).get("male");
        }catch (Exception e){
            e.printStackTrace();
        }


    }
    public static String genFamilyName(){

        return familyNames.get(getIndex(familyNames.size()));
    }

    public static String genName(int sex){
            if(sex ==0 ){
                return femaleNames.get(getIndex(femaleNames.size()));
            }else {
                return maleNames.get(getIndex(maleNames.size()));
            }

    }
    public static int getIndex(int len){
        int times = 0;
        int index = len;
        while(times<2){
            index = new Random().nextInt(index)+1;
            if(index == 1){
                break;
            }
            times++;
        }
        index--;
        return index;
    }
}
